﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.Data;
using RuleCode.Controls;

namespace map.search
{
    public partial class areaitems : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(Request.Params["id"]))
                {
                    Response.Redirect("area.aspx");
                }
                else
                {
                    Bind();
                }
            }
        }

        public string p1 = "";
        public string p2 = "";
        public string p3 = "";
        public  BizArea biz;
        public BizAreaImages bizAreaImages;

        private void Bind( )
        {
            string id = Request["id"];
            string keyvalue = Request["keyvalue"];
            keyvalue = (keyvalue == null) ? "" : keyvalue;
            biz = BizArea.Find("RECID = " + id);                         
            if (biz.Count > 0)
            {
                Page.Title = biz.BizAreaName.ValueString;
                //BizAreaNameSpan.InnerHtml ="商圈 "+ biz.BizAreaName.Value+" 中的商品"; 
                communityTitle.Text = biz.BizAreaName.Value;
                p1 = biz.p1.ToString();
                p2 = biz.p2.ToString();
                p3 = biz.p3.ToString();
                //despLabel.Text = biz.BizAreaDescription.ToString();
                //transparentLabel.Text = biz.Description.ToString();
                DataTable dt;
                string sql = "";
                sql = " select distinct area_p1,area_p2 from Vitem where areaRecid=" + id + " and (taobaotitle like '%" + keyvalue + "%' )";
                dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                DataList_all .DataSource = dt;
                DataList_all.DataBind();


                bizAreaImages = BizAreaImages.Find("BizAreaID='" + id + "'");
                this.DetailImgsDataList.DataSource = bizAreaImages.MyTable;
                this.DetailImgsDataList.DataBind();
            }
        }

        protected void DataList_all_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            string id = Request["id"];
            string keyvalue = Request["keyvalue"];
            keyvalue = (keyvalue == null) ? "" : keyvalue;
            DataTable dt;
            string sql = "";
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                string _p1 = ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString();
                string _p2 = ((System.Data.DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString();
                sql = " select * from Vitem where area_p1 = '" + _p1 + "' and area_p2 = '" + _p2 + "' and "
                    + "areaRecid=" + id + " and (taobaotitle like '%" + keyvalue + "%'  )";
                dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
                
                //WebPager WebPager_in = e.Item.FindControl("WebPager_in") as WebPager;
                //WebPager_in.ControlToPaginate = "DataList_in";
                //WebPager_in.DataSource = dt;
                //WebPager_in.DataBind();

                DataList DataList_in = e.Item.FindControl("DataList_in") as DataList;
                DataList_in.DataSource = dt;
                DataList_in.DataBind();

            }
        }
        protected string GetAreaTitle(string p1s, string p2s)
        {
            if (p2s == string.Empty && p1s == string.Empty)
            {
                return "未定义分区";
            }
            else
            {
                return p1s + p1 + " " + p2s + p2;
            }
        }

        //protected void WebPager_all_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        //{
        //    WebPager_all.CurrentPageIndex = e.NewPageIndex;
        //    Bind();
        //}

        //protected void WebPager_in_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        //{
        //    WebPager curWebPager = (WebPager )sender;
        //    for (int i = 0; i < DataList_all.Items.Count; i++)
        //    {
        //        WebPager webpager = DataList_all.Items[i].FindControl("WebPager_in") as WebPager;
        //        if (curWebPager == webpager)
        //        {
        //            string id = Request["id"];
        //            string keyvalue = Request["keyvalue"];
        //            keyvalue = (keyvalue == null) ? "" : keyvalue;
        //            DataTable dt;
        //            string sql = "";
        //            string _p1 = ((System.Data.DataRowView)DataList_all.Items[i].DataItem).Row.ItemArray[0].ToString();
        //            string _p2 = ((System.Data.DataRowView)DataList_all.Items[i].DataItem).Row.ItemArray[1].ToString();
        //            sql = " select * from Vitem where area_p1 = '" + _p1 + "' and area_p2 = '" + _p2 + "' and "
        //                + "areaRecid=" + id + " and (taobaotitle like '%" + keyvalue + "%' or taobaodesc like '%" + keyvalue + "%')";
        //            dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];

        //            curWebPager.DataSource = dt;
        //            curWebPager.DataBind();
        //            return;
        //        }
        //    }
        //}
    }
}
